
#' Plot protein numbers
#'
#' \code{plot_numbers} generates a barplot
#' of the number of identified proteins per sample.
#'
#' @param se SummarizedExperiment,
#' Data object for which to plot protein numbers
#' (output from \code{\link{make_se}()} or \code{\link{make_se_parse}()}).
#' @param plot Logical(1),
#' If \code{TRUE} (default) the barplot is produced.
#' Otherwise (if \code{FALSE}), the data which the
#' barplot is based on are returned.
#' @param features_type Character(1), the type of features used in title, like 'proteins', 'genes',
#' 'peptides', default is 'features'
#' @return Barplot of the number of identified proteins per sample
#' (generated by \code{\link[ggplot2]{ggplot}})
#' @examples
#' # Load example
#' data(Silicosis_pg)
#' data <- Silicosis_pg
#' data_unique <- make_unique(data, "Gene.names", "Protein.IDs", delim = ";")
#'
#' # Make SummarizedExperiment
#' ecols <- grep("LFQ.", colnames(data_unique))
#'
#'
#' ## Load experiement design
#' data(Silicosis_ExpDesign)
#' exp_design <- Silicosis_ExpDesign
#' se <- make_se(data_unique, ecols, exp_design)
#'
#' # Filter and normalize
#' filt <- filter_se(se, thr = 0, fraction = 0.4, filter_formula = ~ Reverse != "+" & Potential.contaminant!="+")
#'
#' plot_numbers(filt)
#' @export
plot_numbers <- function (se, plot = TRUE, features_type = "proteins")
{
  assertthat::assert_that(inherits(se, "SummarizedExperiment"),
                          is.logical(plot), length(plot) == 1)
  df <- assay(se) %>% data.frame() %>% rownames_to_column() %>%
    gather(ID, bin, -rowname) %>% mutate(bin = ifelse(is.na(bin),
                                                      0, 1))
  stat <- df %>% group_by(ID) %>% summarize(n = n(), sum = sum(bin)) %>%
    left_join(., data.frame(colData(se)), by = "ID")

  firstup <- function(x) {
    substr(x, 1, 1) <- toupper(substr(x, 1, 1))
    x
  }

  p <- ggplot(stat, aes(x = ID, y = sum, fill = condition)) +
    geom_col() + geom_hline(yintercept = unique(stat$n)) +
    labs(title = firstup(paste0(features_type,  " per sample")), x = "",
         y = paste0("Number of ", features_type)) + theme_DEP2()
  if (plot) {
    return(p)
  }
  else {
    df <- as.data.frame(stat)
    colnames(df)[seq_len(3)] <- c("sample", paste0("total_", features_type),
                                  paste0(features_type, "_in_sample"))
    return(df)
  }
}

#' Plot protein coverage
#'
#' \code{plot_coverage} generates a barplot
#' of the protein coverage in all samples.
#'
#' @param se SummarizedExperiment,
#' Data object for which to plot observation frequency.
#' @param plot Logical(1),
#' If \code{TRUE} (default) the barplot is produced.
#' Otherwise (if \code{FALSE}), the data which the
#' barplot is based on are returned.
#' @param features_type Character(1), the type of features used in title, like 'proteins', 'genes',
#' 'peptides', default is 'features'
#' @return Barplot of protein coverage in samples
#' (generated by \code{\link[ggplot2]{ggplot}})
#' @examples
#' # Load example
#' data(Silicosis_pg)
#' data <- Silicosis_pg
#' data_unique <- make_unique(data, "Gene.names", "Protein.IDs", delim = ";")
#'
#' # Make SummarizedExperiment
#' ecols <- grep("LFQ.", colnames(data_unique))
#'
#'
#' ## Load experiement design
#' data(Silicosis_ExpDesign)
#' exp_design <- Silicosis_ExpDesign
#' se <- make_se(data_unique, ecols, exp_design)
#'
#' # Filter and normalize
#' filt <- filter_se(se, thr = 0, fraction = 0.4, filter_formula = ~ Reverse != "+" & Potential.contaminant!="+")
#'
#' plot_coverage(filt)
#' @export
plot_coverage <- function (se, plot = TRUE, features_type = "features")
{
  assertthat::assert_that(inherits(se, "SummarizedExperiment"),
                          is.logical(plot), length(plot) == 1)
  df <- assay(se) %>% data.frame() %>% rownames_to_column() %>%
    gather(ID, bin, -rowname) %>% mutate(bin = ifelse(is.na(bin),
                                                      0, 1))
  stat <- df %>% group_by(rowname) %>% summarize(sum = sum(bin))
  table <- table(stat$sum) %>% data.frame()
  firstup <- function(x) {
    substr(x, 1, 1) <- toupper(substr(x, 1, 1))
    x
  }

  p <- ggplot(table, aes(x = "all", y = Freq, fill = Var1)) +
    geom_col(col = "white") + scale_fill_grey(start = 0.8,
                                              end = 0.2) + labs(title = firstup(paste0(substr(features_type, 1, (nchar(features_type) -1)), " coverage")), x = "",
                                                                y = paste0("Number of ", features_type), fill = "Samples") +
    theme_DEP1()
  if (plot) {
    return(p)
  }
  else {
    df <- as.data.frame(table)
    colnames(df) <- c("samples", features_type)
    return(df)
  }
}




